﻿namespace eProject.ViewModel.Commands
{
    public class MoveScheduleItemCommand : ICommand
    {
        private bool _isCanceled;
        private bool _isExecuted;
        private readonly CommonManager _manager;
        private readonly ScheduleItemViewModel _item;
        private readonly int _newIndex;
        private int _oldIndex;

        public MoveScheduleItemCommand(CommonManager manager, ScheduleItemViewModel item, int newIndex)
        {
            _manager = manager;
            _item = item;
            _newIndex = newIndex;          
        }

        public void Execute()
        {
            if (_isExecuted) return;

            _oldIndex = _manager.ScheduleItems.IndexOf(_item);
            _manager.MoveScheduleItem(_item, _newIndex);
                
            _isExecuted = true;
            _isCanceled = false;
        }

        public void Unexecute()
        {
            if (_isCanceled) return;

            _manager.MoveScheduleItem(_item, _oldIndex);

            _isCanceled = true;
            _isExecuted = false;
        }
    }
}
